#!/bin/bash

echo "=== Kafka 大批量数据处理性能测试 ==="
echo ""

BASE_URL="http://localhost:8080/api/kafka"

# 检查应用是否运行
echo "1. 检查应用状态..."
if ! curl -s "$BASE_URL/health" > /dev/null; then
    echo "错误: 应用未运行，请先启动应用 (mvn spring-boot:run)"
    exit 1
fi

echo "应用运行正常"
echo ""

# 重置统计信息
echo "2. 重置统计信息..."
curl -s -X POST "$BASE_URL/reset-stats" | jq .
echo ""

# 测试不同批量大小
echo "3. 开始性能测试..."
echo ""

# 测试1000条数据
echo "测试 1000 条数据:"
start_time=$(date +%s)
curl -s -X POST "$BASE_URL/send-batch?count=1000" | jq .
end_time=$(date +%s)
duration=$((end_time - start_time))
echo "耗时: ${duration}秒"
echo ""

sleep 5

# 测试5000条数据
echo "测试 5000 条数据:"
start_time=$(date +%s)
curl -s -X POST "$BASE_URL/send-batch?count=5000" | jq .
end_time=$(date +%s)
duration=$((end_time - start_time))
echo "耗时: ${duration}秒"
echo ""

sleep 5

# 测试10000条数据
echo "测试 10000 条数据:"
start_time=$(date +%s)
curl -s -X POST "$BASE_URL/send-batch?count=10000" | jq .
end_time=$(date +%s)
duration=$((end_time - start_time))
echo "耗时: ${duration}秒"
echo ""

sleep 5

# 异步测试50000条数据
echo "异步测试 50000 条数据:"
start_time=$(date +%s)
curl -s -X POST "$BASE_URL/send-batch-async?count=50000" | jq .
end_time=$(date +%s)
duration=$((end_time - start_time))
echo "启动耗时: ${duration}秒"
echo ""

sleep 10

# 获取最终统计信息
echo "4. 获取处理统计信息..."
curl -s -X GET "$BASE_URL/stats" | jq .
echo ""

echo "=== 性能测试完成 ==="
echo ""
echo "提示:"
echo "- 查看Kafka UI: http://localhost:8080 (如果使用Docker)"
echo "- 查看应用日志了解详细处理信息"
echo "- 可以调整application.properties中的配置来优化性能"
echo ""
